<template>
  <div class="login">
    <h1>欢迎登录</h1>
    <div class="form">
      <div class="item">
        <span class="iconfont icon-jurassic_user"></span>
        <input type="text" placeholder="手机号码" v-model="phone" />
      </div>
      <div class="item">
        <span class="iconfont icon-mima"></span>
        <input type="password" placeholder="密码" v-model="password" />
      </div>
      <div class="item">
        <span class="iconfont icon-yanzhengma"></span>
        <input type="text" placeholder="验证码" v-model="code" />
        <button class="getCode" @click="getCode">发送</button>
      </div>
      <div class="item">
        <div class="submit" @click="submit">提交</div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCaptcha, getLoginCellphone } from "../../api/login";
export default {
  data() {
    return {
      code: "",
      phone: "",
      password: "",
    };
  },
  methods: {
    // 获取验证码
    getCode() {
      // 1.验证手机号码
      let reg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (!reg.test(this.phone)) {
        // 不正确处理
        this.$toast({ message: "手机号码错误", position: "bottom" });
        return; //停止页面操作
      }
      // 2.请求数据接口
      getCaptcha({ phone: this.phone }).then((data) => {
        console.log(data);
        if (data.data) {
          this.$toast({ message: "验证码发送成功", position: "bottom" });
        } else {
          this.$toast({ message: "请输入正确手机号码", position: "bottom" });
        }
      });
    },
    // 提交数据
    submit() {
      // 1.验证验证码
      // let reg = /^[0-9]{4}$/;
      // if(!reg.test(this.code)){
      //     this.$toast({message:"请输入正确验证码",position:"bottom"});
      //     return ; //停止页面操作
      // }
      // getCaptchaVerify({
      //     phone:this.phone,
      //     captcha:this.code
      // }).then(data=>{
      //     if(!data.data){
      //         this.$toast({message:"请输入正确验证码",position:"bottom"});
      //     }else{
      // 请求用户名密码登录
      getLoginCellphone({
        phone: this.phone,
        password: this.password,
      }).then((data) => {
        console.log(data, "登录");
        if (data.code == 200) {
          this.$toast({ message: "登录成功", position: "bottom" });
          //判断是否已登录
          localStorage.setItem("token", data.token); //登录成功添加token
          this.$router.push("/home");
        } else {
          this.$toast({ message: "登录失败", position: "bottom" });
        }
      });
    },
  },
};
</script>


<style lang="less">
.login {
  background-color: #d4473c;
  width: 100%;
  height: 100%;
  color: #fff;
}
h1 {
  font-size: 48px;
  text-align: center;
  height: 48px;
  padding: 150px 0 40px 0;
}
.form {
  .item {
    width: 76%;
    height: 30px;
    line-height: 30px;
    margin: 0 auto;
    background-color: #f47378;
    border-radius: 30px;
    margin-bottom: 12px;
    input {
      background-color: transparent;
      border: 0px;
      outline: none;
      text-indent: 0.5em;
      font-size: 14px;
      color: #fff;
    }
    span {
      font-size: 20px;
      margin-left: 10px;
      display: inline-block;
      vertical-align: middle;
    }
    .getCode {
      border: 1px solid #fff;
      height: 20px;
      line-height: 20px;
      width: 50px;
      border-radius: 20px;
      font-size: 12px;
      color: #808080;
      float: right;
      margin: 5px;
    }
    .submit {
      font-size: 15px;
      background-color: #fff;
      text-align: center;
      color: #d4473c;
      border-radius: 15px;
    }
  }
}
</style>